package com.megvii.corelib.util;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * Created by linyue on 16/3/15.
 */
public class ScreenUtil {
    private static ScreenUtil instance;

    //屏幕的宽高, 单位像素
    public int screenWidth;

    public int screenHeight;

    //屏幕的密度
    private float density;  //只有五种情况 : 0.75/ 1.0/ 1.5/ 2.0/ 3.0

    private int densityDpi; //只有五种情况 : 120/ 160/ 240/ 320/ 480

    //水平垂直精确密度
    private float xdpi; //水平方向上的准确密度, 即每英寸的像素点

    private float ydpi; //垂直方向上的准确密度, 即没音村的像素点

    private Context context;

    private ScreenUtil() {

    }

    public static ScreenUtil getInstance() {
        if (instance == null) {
            instance = new ScreenUtil();
        }
        return instance;
    }

    public void init(Context context) {
        this.context = context;
        getPixelDisplayMetrics();
        Log.d("qwe", "model" + android.os.Build.MODEL);
        Log.d("qwe", "宽:" + screenWidth + ", 高:" + screenHeight);
        Log.d("qwe", "密度 density:" + density + ",densityDpi:" + densityDpi);
        Log.d("qwe", "精确密度 xdpi:" + xdpi + ", ydpi:" + ydpi);
    }

    //    private void getPixelWindowManager()
    //    {
    //        Point size = new Point();
    //        context.getWindowManager().getDefaultDisplay().getSize(size);
    //        screenWidth = size.x;
    //        screenHeight = size.y;
    //    }

    private void getPixelDisplayMetrics() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();

        screenWidth = dm.widthPixels;
        //判断产品型号
        if (Build.MODEL.equals("rk3288") || Build.MODEL.equals("IF236a")) {
            screenHeight = dm.heightPixels + getNavigationBarHeight(context);
        } else {
            screenHeight = dm.heightPixels;
        }

        density = dm.density;
        densityDpi = dm.densityDpi;

        xdpi = dm.xdpi;
        ydpi = dm.ydpi;
    }


    public static int getNavigationBarHeight(Context activity) {
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");
        //获取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    //    private void getPixelDisplayMetricsII()
    //    {
    //        DisplayMetrics dm = new DisplayMetrics();
    //        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    //
    //        screenWidth = dm.widthPixels;
    //        screenHeight = dm.heightPixels;
    //
    //        density = dm.density;
    //        densityDpi = dm.densityDpi;
    //
    //        xdpi = dm.xdpi;
    //        ydpi = dm.ydpi;
    //    }
}
